package com.itheima.stock.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.stock.common.domian.RoleAddDomain;
import com.itheima.stock.common.domian.UserPageDomain;
import com.itheima.stock.service.RolesService;
import com.itheima.stock.vo.req.UserMenus;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: siwukun
 * @date: 2022/06/10
 * @description:
 */
@RestController
@RequestMapping("/api")
public class RolesController {

    @Autowired
    RolesService rolesService;

    @PostMapping("/roles")
    public R<PageResult<PageInfo>> getUserByPageInfo(@RequestBody UserPageDomain userPageDomain) {
        return rolesService.getUserByPageInfo(userPageDomain);
    }

    /**
     * 添加角色回显权限选项功能
     *
     * @return
     */
    @GetMapping("/permissions/tree/all")
    public R<List<UserMenus>> getPermissionEcho() {
        return rolesService.getPermissionEcho();
    }

    /**
     * 添加角色和角色关联
     *
     * @param roleAddDomain
     * @return
     */
    @PostMapping("/role")
    public R<String> addRoles(@RequestBody RoleAddDomain roleAddDomain) {
        return rolesService.addRoles(roleAddDomain);
    }

    /**
     *  根据角色id查找对应的权限id集合
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/role/{roleId}")
    public R<List<String>> getAllPid(@PathVariable String roleId){
        return rolesService.getAllPid(roleId);
    }

    /**
     * 更新角色信息，包含角色关联的权限信息
     *
     * @param roleAddDomain
     * @return
     */
    @PutMapping("/role")
    public R<String> updateRoleByRoleId(@RequestBody RoleAddDomain roleAddDomain){
        return rolesService.updateRoleByRoleId(roleAddDomain);
    }


    /**
     * 根据角色id删除角色信息
     * @param roleId 角色id
     * @return
     */
    @DeleteMapping("/role/{roleId}")
    public R<String> deleteFormById(@PathVariable String roleId){
        return rolesService.deleteFormById(roleId);
    }

    /**
     * 更新角色的状态信息
     * @param roleId 角色id
     * @param status 状态码
     * @return
     */
    @PostMapping("/role/{roleId}/{status}")
    public R<String> updateRoleStatus(@PathVariable String roleId,
                                      @PathVariable Integer status){
        return rolesService.updateRoleStatus(roleId,status);
    }
}
